import cv2
import os

def decode_qr_codes_with_opencv(image_folder, output_file='qr_codes_opencv.txt'):
    """
    使用OpenCV识别二维码（需要安装opencv-contrib-python）
    """
    # 初始化QRCode检测器
    qr_decoder = cv2.QRCodeDetector()
    
    image_files = [f for f in os.listdir(image_folder) 
                  if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp'))]
    
    results = []
    
    for img_file in sorted(image_files):
        img_path = os.path.join(image_folder, img_file)
        try:
            # 读取图片
            img = cv2.imread(img_path)
            
            # 检测并解码二维码
            data, bbox, _ = qr_decoder.detectAndDecode(img)
            
            if data:
                results.append(f"{data}")
                print(f"识别成功: {img_file} -> {data}")
            else:
                print(f"未在 {img_file} 中检测到二维码")
                
        except Exception as e:
            print(f"处理 {img_file} 时出错: {str(e)}")
    
    # 保存结果
    print("".join(results))
    
    print(f"识别完成，结果已保存到 {output_file}")

dir_path = "frames"

# 使用示例
decode_qr_codes_with_opencv(dir_path)